﻿function addTab(title, id, url)
{
    if ($('#' + id).tabs('exists', title))
    {
        $('#' + id).tabs('select', title);
        updateTab(id, url);
    }
    else
    {
        var iscloseable = true;
        iscloseable = ((title == "首页") ? false : true);
        $('#' + id).tabs('add', {
            title: title,
            cache: true,
            href: url,
            closable: iscloseable
        });
    }
}

function updateTab(id, url)
{
    var tab = $('#' + id).tabs('getSelected');
    $("#" + id).tabs('update', {
        tab: tab,
        options: {
            href: url
        }
    });
    tab.panel('refresh');
}

function closeAllTabs(id)
{
    var tabs = $("#" + id).tabs("tabs");
    while (tabs.length > 1)
    {
        var onetab = tabs[tabs.length - 1];
        var title = onetab.panel('options').tab.text();
        $("#" + id).tabs("close", title);
    }
}

function toggleHeard()
{
    $('.form_heard').toggle();
}

//function openWindow(url)
//{
//   this.window.open(url,','','width=530,height=200,location=no,menubar=no,status=no,scrollbars=no,resizable=no top=300,left=500');
//}

$(document).keydown(function (e)
{
    var target = e.target;
    var tag = e.target.tagName.toUpperCase();
    if (e.keyCode == 8)
    {
        if ((tag == 'INPUT' && !$(target).attr("readonly")) || (tag == 'TEXTAREA' && !$(target).attr("readonly")))
        {
            if ((target.type.toUpperCase() == "RADIO") || (target.type.toUpperCase() == "CHECKBOX"))
            {
                return false;
            } else
            {
                return true;
            }
        } else
        {
            return false;
        }
    }
});


function resizeWindow(idname)
{
    $('#' + idname).window('resize', {
        width: 600,
        height: 300
    });
}

function openWindow(idname)
{
    $('#' + idname).window('open');
}

function closeWindow(idname)
{
    $('#' + idname).window('close');
}

function showMessage(title, msg, timeout,showType)
{
    $.messager.show({
        title: title,
        msg: msg,
        timeout: timeout,
        showType: showType
    });
}

function close_msg(obj)
{
    $(obj).clearQueue();
    $(obj).hide();
}

function keep_msg(obj)
{
    $(obj).clearQueue();
    $(obj).fadeTo("fast", 1);
}

function move_msg(obj)
{
    close_msg(obj);
}

function showLabelMessage(obj,msg)
{
    $(obj).attr('display', 'inline');
    $(obj).html('<u onmouseout="move_msg(\'' + obj + '\')" onmouseover="keep_msg(\'' + obj + '\')"><li class="icon1"></li><li class="icon2">' + msg + '</li><li class="icon3" onclick="close_msg(\'' + obj + '\')"></li></u>');
    $(obj).fadeTo("slow", 1);
    $(obj).fadeTo("slow", 0.7);
    $(obj).fadeTo("slow", 0.4);
    $(obj).fadeTo("slow", 0.15);
    $(obj).fadeTo("slow", 0);
}

function InitSelectStock(currentStockTable)
{
    $('#' + currentStockTable).datagrid({
        columns: [[
            { field: 'IsSelected', title: '选择', width: 60, checkbox: true },
            { field: 'Type', title: '品类名称', width: 60 },
            { field: 'NoPrefixId', title: '存货编码', width: 100 },
            { field: 'Id', title: '存货编码', width: 100, hidden: true },
            { field: 'BarCode', title: '条形码', width: 80 },
            { field: 'Name', title: '存货名称', width: 80 },
            { field: 'Specification', title: '规格', width: 60 },
            { field: 'UnitTransfer', title: '计量单位', width: 100 },
            { field: 'LeftNumber', title: '现存量', width: 100 },
            { field: 'StandardPurchasePrice', title: '报价', width: 80 }
        ]],
        checkOnSelect: true,
        selectOnCheck: true
    });
}

function InitSelectStockForSales(currentStockTable)
{
    $('#' + currentStockTable).datagrid({
        columns: [[
            { field: 'IsSelected', title: '选择', width: 60, checkbox: true },
            { field: 'Type', title: '品类名称', width: 60 },
            { field: 'NoPrefixId', title: '存货编码', width: 100 },
            { field: 'Id', title: '存货编码', width: 100, hidden: true },
            { field: 'BarCode', title: '条形码', width: 80 },
            { field: 'Name', title: '存货名称', width: 80 },
            { field: 'Specification', title: '规格', width: 60 },
            { field: 'UnitTransfer', title: '计量单位', width: 100 },
            { field: 'StandardSalesPrice', title: '报价', width: 80 }
        ]],
        checkOnSelect: true,
        selectOnCheck: true
    });
}

function InitSelectCustomer(currentCustomerTable)
{
    $('#' + currentCustomerTable).datagrid({
        columns: [[
            { field: 'IsSelected', title: '选择', width: 60, checkbox: true },
            { field: 'NoPrefixId', title: '客户编号', width: 100 },
            { field: 'Id', title: '客户编号', width: 100, hidden: true },
            { field: 'Name', title: '客户名称', width: 80 },
            { field: 'Contact', title: '联系人', width: 60 }
        ]],
        singleSelect: true,
        checkOnSelect: true,
        selectOnCheck: true
    });
}

function InitSelectIndustryType(currentIndustryTypeTable)
{
    $('#' + currentIndustryTypeTable).datagrid({
        columns: [[
            { field: 'IsSelected', title: '选择', width: 60, checkbox: true },
            { field: 'Id', title: '编号', width: 100 },
            { field: 'ChinesName', title: '行业名称(中文)', width: 80 },
            { field: 'EnglishName', title: '行业名称(英文)', width: 80 }
        ]],
        singleSelect: true,
        checkOnSelect: true,
        selectOnCheck: true
    });
}

function InitSelectSupplierType(currentSupplierTypeTable)
{
    $('#' + currentSupplierTypeTable).datagrid({
        toolbar: '#supplierTypetb',
        url: "/SupplierType/list",
        columns: [[
            { field: 'IsSelected', title: '选择', width: 60, checkbox: true },
            { field: 'Id', title: '编号', width: 100 },
            { field: 'Name', title: '供应商类别', width: 80 }
        ]],
        singleSelect: true,
        checkOnSelect: true,
        selectOnCheck: true
    });
}

function InitSelectSupplier(currentSupplierTable)
{
    $('#' + currentSupplierTable).datagrid({
        columns: [[
            { field: 'IsSelected', title: '选择', width: 60, checkbox: true },
            { field: 'NoPrefixId', title: '供应商编号', width: 100 },
            { field: 'Id', title: '供应商编号', width: 100, hidden: true },
            { field: 'Name', title: '供应商名称', width: 80 },
            { field: 'Contact', title: '联系人', width: 60 }
        ]],
        singleSelect: true,
        checkOnSelect: true,
        selectOnCheck: true
    });
}

function InitSelectEmployee(currentEmployeeTable)
{
    $('#' + currentEmployeeTable).datagrid({
        columns: [[
            { field: 'IsSelected', title: '选择', width: 60, checkbox: true },
            { field: 'NoPrefixId', title: '职员编号', width: 100 },
            { field: 'Id', title: '职员编号', width: 100, hidden: true },
            { field: 'Name', title: '职员名称', width: 80 }
        ]],
        singleSelect: true,
        checkOnSelect: true,
        selectOnCheck: true
    });
}

function InitSelectWareHouse(currentWareHouseTable)
{
    $('#' + currentWareHouseTable).datagrid({
        columns: [[
            { field: 'IsSelected', title: '选择', width: 60, checkbox: true },
            { field: 'Address', title: '存货地点', width: 80 }
        ]],
        checkOnSelect: true,
        selectOnCheck: true
    });
}

function InitSelectDepartment(currentDepartmentTable)
{
    $('#' + currentDepartmentTable).datagrid({
        columns: [[
            { field: 'IsSelected', title: '选择', width: 60, checkbox: true },
            { field: 'NoPrefixId', title: '部门编号', width: 100 },
            { field: 'Id', title: '部门编号', width: 100, hidden: true },
            { field: 'Name', title: '名称', width: 80 },
            { field: 'SuperiorDepartment', title: '上级部门', width: 60 }
        ]],
        singleSelect: true,
        checkOnSelect: true,
        selectOnCheck: true
    });
}

function InitSelectAccount(currentAccountTable) {
    $('#' + currentAccountTable).datagrid({
        columns: [[
            { field: 'IsSelected', title: '选择', width: 60, checkbox: true },
            { field: 'NoPrefixId', title: '编号', width: 100 },
            { field: 'Id', title: '编号', width: 100, hidden: true },
            { field: 'Name', title: '名称', width: 80 }
        ]],
        singleSelect: true,
        checkOnSelect: true,
        selectOnCheck: true
    });
}

$(function ()
{
    $.extend($.fn.validatebox.defaults.rules, {
        minLength: {
            validator: function (value, param)
            {
                return value.length >= param[0];
            },
            message: 'Please enter at least {0} characters.'
        }
    });
})